<?php

/**
 * 省市区
 */
if (!function_exists('fish_addr_area')) {
    function fish_addr_area()
    {
        $data['provinceData'] = \App\Models\AreaModel::where(['level' => 1])->orderBy('id', 'asc')->get(['id as value', 'name as label', 'pid', 'level']);
        foreach ($data['provinceData'] as $k => $p) {
            $data['cityData'][$k] = \App\Models\AreaModel::where(['level' => 2, 'pid' => $p->value])->orderBy('id', 'asc')->select(['id as value', 'name as label', 'pid', 'level'])->get();
            foreach ($data['cityData'][$k] as $i => $c) {
                $data['areaData'][$k][$i] = \App\Models\AreaModel::where(['level' => 3, 'pid' => $c->value])->orderBy('id', 'asc')->select(['id as value', 'name as label', 'pid', 'level'])->get();
            }
        }
        return $data;
    }
}

/**
 * 用户地址列表
 * @param $userId int
 * @param $page int
 * @param $size int
 */
if (!function_exists('fish_addr_list')) {
    function fish_addr_list($userId, $page, $size)
    {
        $data = \App\Models\AddrModel::where(['user_id' => $userId])
            ->orderBy('is_default', 'desc')
            ->orderBy('created_at', 'asc')
            ->select(['id', 'is_default', 'user_id', 'consignee', 'phone', 'province_name', 'city_name', 'area_name', 'address', 'created_at'])
            ->Paginate($size, ['*'], 'page', $page);
        return $data;
    }
}

/**
 * 用户默认地址
 * @param int $userId
 */
if (!function_exists('fish_addr_def')) {
    function fish_addr_def($userId)
    {
        $data = \App\Models\AddrModel::where(['user_id' => $userId, 'is_default' => '1'])
            ->select(['id', 'is_default', 'user_id', 'consignee', 'phone', 'province_name', 'city_name', 'area_name', 'address', 'created_at'])
            ->first();
        return $data;
    }
}

/**
 * 修改 添加地址
 * @param array $formData
 * @param int $userId
 */
if (!function_exists('fish_addr_edit')) {
    function fish_addr_edit($formData, $userId)
    {
        $areaNameArray = explode("-", $formData['area_text']);
        try {
            $province = \App\Models\AreaModel::where(['name' => $areaNameArray[0], 'level' => 1])->first();
            $city = \App\Models\AreaModel::where(['name' => $areaNameArray[1], 'level' => 2])->first();
            $area = \App\Models\AreaModel::where(['name' => $areaNameArray[2], 'pid' => $city->id, 'level' => 3])->first();
            if (!$province || !$city || !$area) {
                fish_fail('暂不支持,请手动选择行政区');
            }
        } catch (\Exception $e) {
            fish_fail('暂不支持,请手动选择行政区');
        }

        $is_default = (isset($is_default) && $is_default) ? '1' : '0';
        if ($is_default) {
            \App\Models\AddrModel::where(['user_id' => $userId, 'is_default' => '1'])->update(['is_default' => '0']);
        }
        $saveData = [
            'is_default' => $formData['is_default'],
            'phone' => $formData['phone'],
            'province_name' => $areaNameArray[0],
            'city_name' => $areaNameArray[1],
            'area_name' => $areaNameArray[2],
            'province_id' => $province->id,
            'city_id' => $city->id,
            'area_id' => $area->id,
            'user_id' => $userId,
            'address' => $formData['address'],
            'consignee' => $formData['consignee'],
        ];
        if (!isset($formData['id']) || (int)$formData['id'] === 0) {
            $res = \App\Models\AddrModel::create($saveData);
        } else {
            $res = \App\Models\AddrModel::where(['id' => $formData['id']])->update($saveData);
        }
        return $res;
    }
}

/**
 * 用户收货地址详情
 * @param int $id
 * @param int $userId
 */
if (!function_exists('fish_addr_info')) {
    function fish_addr_info($id, $userId)
    {
        $data = \App\Models\AddrModel::where(['id' => $id, 'user_id' => $userId])->first();
        return $data;
    }
}

/**
 * 删除收货地址
 * @param int $id
 * @param int $userId
 */
if (!function_exists('fish_addr_del')) {
    function fish_addr_del($id, $userId)
    {
        $data = \App\Models\AddrModel::where(['id' => $id, 'user_id' => $userId])->first();
        return $data;
    }
}